home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / bbsutil / bsrc_250.zip / FILE_DOS.C < prev    next >
C/C++ Source or Header  |  1991-09-15  |  17KB  |  514 lines

  1. /*--------------------------------------------------------------------------*/
  2. /*                                                                          */
  3. /*                                                                          */
  4. /*      ------------         Bit-Bucket Software, Co.                       */
  5. /*      \ 10001101 /         Writers and Distributors of                    */
  6. /*       \ 011110 /          Freely Available<tm> Software.                 */
  7. /*        \ 1011 /                                                          */
  8. /*         ------                                                           */
  9. /*                                                                          */
  10. /*  (C) Copyright 1987-91, Bit Bucket Software Co., a Delaware Corporation. */
  11. /*                                                                          */
  12. /*                                                                          */
  13. /*                This module was written by Vince Perriello                */
  14. /*                   OS/2 code contributed by Bill Andrus                   */
  15. /*                                                                          */
  16. /*            DOS and OS/2 File I/O routines used by BinkleyTerm            */
  17. /*                                                                          */
  18. /*                                                                          */
  19. /*    For complete  details  of the licensing restrictions, please refer    */
  20. /*    to the License  agreement,  which  is published in its entirety in    */
  21. /*    the MAKEFILE and BT.C, and also contained in the file LICENSE.250.    */
  22. /*                                                                          */
  23. /*    USE  OF THIS FILE IS SUBJECT TO THE  RESTRICTIONS CONTAINED IN THE    */
  24. /*    BINKLEYTERM  LICENSING  AGREEMENT.  IF YOU DO NOT FIND THE TEXT OF    */
  25. /*    THIS  AGREEMENT IN ANY OF THE  AFOREMENTIONED FILES,  OR IF YOU DO    */
  26. /*    NOT HAVE THESE FILES,  YOU  SHOULD  IMMEDIATELY CONTACT BIT BUCKET    */
  27. /*    SOFTWARE CO.  AT ONE OF THE  ADDRESSES  LISTED BELOW.  IN NO EVENT    */
  28. /*    SHOULD YOU  PROCEED TO USE THIS FILE  WITHOUT HAVING  ACCEPTED THE    */
  29. /*    TERMS  OF  THE  BINKLEYTERM  LICENSING  AGREEMENT,  OR  SUCH OTHER    */
  30. /*    AGREEMENT AS YOU ARE ABLE TO REACH WITH BIT BUCKET SOFTWARE, CO.      */
  31. /*                                                                          */
  32. /*                                                                          */
  33. /* You can contact Bit Bucket Software Co. at any one of the following      */
  34. /* addresses:                                                               */
  35. /*                                                                          */
  36. /* Bit Bucket Software Co.        FidoNet  1:104/501, 1:343/491             */
  37. /* P.O. Box 460398                AlterNet 7:491/0                          */
  38. /* Aurora, CO 80046               BBS-Net  86:2030/1                        */
  39. /*                                Internet f491.n343.z1.fidonet.org         */
  40. /*                                                                          */
  41. /* Please feel free to contact us at any time to share your comments about  */
  42. /* our software and/or licensing policies.                                  */
  43. /*                                                                          */
  44. /*--------------------------------------------------------------------------*/
  45.  
  46. /* Include this file before any other includes or defines! */
  47.  
  48. #include "includes.h"
  49.  
  50. #ifdef NEED_MKTEMP
  51. char *mktemp (char *template)
  52. {
  53.    static char save[8] = "Z";
  54.    char *p;
  55.    int i;
  56.  
  57.    p = save;
  58.  
  59.    if (*p == 'Z')
  60.       sprintf (p, "%06d", TaskNumber);
  61.    while (*p)
  62.       {
  63.       if (isdigit (*p))
  64.          {
  65.          *p = 'a';
  66.          break;
  67.          }
  68.       if (*p++ < 'z')
  69.          {
  70.          break;
  71.          }
  72.       }
  73.    if ((*p == '\0') || ((i = strlen (template)) < 6))
  74.       return (NULL);
  75.    strcpy (&template[i-6], save);
  76.    return (template);
  77. }
  78. #endif
  79.  
  80. #ifdef NEED_DOSREAD
  81. int _dos_read (int fd, void far *buf, unsigned nbytes, unsigned int *bytes_read)
  82. {
  83.     union REGS regs;
  84.     struct SREGS sregs;
  85.  
  86.     regs.h.ah = 0x3f;    /* read file */
  87.     regs.x.bx = fd;
  88.     regs.x.cx = nbytes;
  89.     regs.x.dx = FP_OFF (buf);
  90.     sregs.ds = FP_SEG (buf);
  91.     *bytes_read = intdosx (®s, ®s, &sregs);
  92.     return regs.x.cflag ? -1 : 0;
  93. }
  94.  
  95. int _dos_write (int fd, void far *buf, unsigned nbytes, unsigned int *bytes_read)
  96. {
  97.     union REGS regs;
  98.     struct SREGS sregs;
  99.  
  100.     regs.h.ah = 0x40;    /* write file */
  101.     regs.x.bx = fd;
  102.     regs.x.cx = nbytes;
  103.     regs.x.dx = FP_OFF (buf);
  104.     sregs.ds = FP_SEG (buf);
  105.     *bytes_read = intdosx (®s, ®s, &sregs);
  106.     return regs.x.cflag ? -1 : 0;
  107. }
  108. #endif
  109.  
  110. #ifdef __TURBOC__ 
  111. /* 
  112.  * utime function for Turbo / Borland C.
  113.  *
  114.  * We should make this more generic in case some other DOS
  115.  * compiler comes up lacking, but for now the only one we
  116.  * have that needs it happens to be Borland.
  117.  *
  118.  */
  119.  
  120. int cdecl utime (char *name, struct utimbuf *times)
  121. {
  122.     int handle;
  123.     struct date d;
  124.     struct time t;
  125.     struct ftime ft;
  126.  
  127.     unixtodos (times->modtime, &d, &t);
  128.     ft.ft_tsec = t.ti_sec / 2;
  129.     ft.ft_min = t.ti_min;
  130.     ft.ft_hour = t.ti_hour;
  131.     ft.ft_day = d.da_day;
  132.     ft.ft_month = d.da_mon;
  133.     ft.ft_year = d.da_year - 1980;
  134.     if ((handle = open(name, O_RDONLY)) == -1)
  135.         return -1;
  136.  
  137.     setftime (handle, &ft);
  138.     close (handle);
  139.     return 0;
  140. }
  141. #endif
  142.  
  143.  
  144. int dexists (char *filename)
  145. {
  146.    struct stat stbuf;
  147.  
  148.    return (stat (filename, &stbuf) != -1) ? 1 : 0;
  149. }
  150.  
  151. int dfind (struct FILEINFO *dta, char *name, int times)
  152. {
  153. #ifndef OS_2
  154.    union REGS r;
  155.    struct SREGS s;
  156.    char far *dtaptr = (char *)dta;
  157.    char far *nameptr = (char *)name;
  158.  
  159.    s.ds = FP_SEG (dtaptr);
  160.    r.x.dx = FP_OFF (dtaptr);
  161.    r.h.ah = 0x1a;
  162.    (void) intdosx (&r, &r, &s);
  163.    r.x.bx = 0;
  164.    r.x.cx = ~0x08;
  165.    s.ds = FP_SEG (nameptr);
  166.    r.x.dx = FP_OFF (nameptr);
  167.    r.x.si = 0;
  168.    r.x.di = 0;
  169.    if (times == 0)
  170.       {
  171.       r.h.ah = 0x4e;
  172.       (void) intdosx (&r, &r, &s);
  173.       dta->nill = '\0';
  174.       /* If not found or a character device (e.g. "COM1") */
  175.       if ((r.x.cflag != 0) || ((dta->attr & 0x40) != 0))
  176.          {
  177.          dta->name[0] = '\0';
  178.          return (1);
  179.          }
  180.       return (0);
  181.       }
  182.    else
  183.       {
  184.       r.h.ah = 0x4f;
  185.       (void) intdosx (&r, &r, &s);
  186.       dta->nill = '\0';
  187.       /* If not found or a character device (e.g. "COM1") */
  188.       if ((r.x.cflag != 0) || ((dta->attr & 0x40) != 0))
  189.          {
  190.          dta->name[0] = '\0';
  191.          return (1);
  192.          }
  193.       return (0);
  194.       }
  195. #else
  196.    if (times == 0)
  197.       {
  198.       return (dir_findfirst (name, 0x37, dta));
  199.       }
  200.    else
  201.       {
  202.       return (dir_findnext (dta));
  203.       }
  204. #endif
  205. }
  206.  
  207.  
  208. int got_error (char *string1, char *string2)
  209. {
  210.    if (errno == 0x18)
  211.       errno = 0;
  212.    if (errno != 0)
  213.       {
  214.       status_line ("%s %d, %s %s %s", MSG_TXT(M_ERROR), errno, MSG_TXT(M_CANT), string1, string2);
  215.       errno = 0;
  216.       return (1);
  217.       }
  218.    return (0);
  219. }
  220.  
  221.  
  222. /* Z F R E E -- Return total number of free bytes on drive specified */
  223. #ifdef OS_2
  224. long zfree (char *path)
  225. {
  226.    int drive;
  227.    FSALLOCATE dt;
  228.  
  229.    if (*path != '\0' && path[1] == ':')
  230.       drive = tolower (*path) - 'a' + 1;
  231.    else
  232.       drive = 0;
  233.    DosQFSInfo (drive, 1, (char far *) &dt, sizeof (FSALLOCATE));
  234.    return ( dt.cSectorUnit * dt.cUnitAvail * dt.cbSector);
  235. }
  236.  
  237. #else /* OS_2 */
  238.  
  239. long zfree (char *drive)
  240. {
  241.    union REGS r;
  242.  
  243.    unsigned char driveno;
  244.    long stat;
  245.  
  246.    if (drive[0] != '\0' && drive[1] == ':')
  247.       {
  248.       driveno = (unsigned char) (islower (*drive) ? toupper (*drive) : *drive);
  249.       driveno = (unsigned char) (driveno - 'A' + 1);
  250.       }
  251.    else driveno = 0;                             /* Default drive    */
  252.  
  253.    r.x.ax = 0x3600;                              /* get free space   */
  254.    r.h.dl = dr